#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>v[maxn];
LL l1[maxn],r1[maxn];
multiset< pair<LL,LL> >s1,s2;
LL pre1[maxn],suf1[maxn];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
       LL maxv=-1;LL minv=1e18;
       LL l;cin>>l;
       for(LL j=1;j<=l;j++){
          LL x;cin>>x;
          ///v[i].push_back(x);
          if(maxv<x) maxv=x;
          if(minv>x) minv=x;
       }
       l1[i]=minv;r1[i]=maxv;

   }
   LL sum1=0;
   for(LL i=1;i<=n;i++){
       s2.erase(s2.find({r1[i],i}));
       if(l1[i]<r1[i]){
          sum1+=n-i;
       }
       else{
          multiset< pair<LL,LL> >:: iterator it1;  it1=s2.end();it1--;
          multiset<pair<LL,LL>>:: iterator it2;
          it2=s2.lower_bound({l1[i]+1,0});
          sum1+=(it1.second-it2.second)+1;
       }
   }
   LL sum2=0;
   for(LL i=n;i>=1;i--){
       s1.erase(s1.find(r1[i]));
       if(l1[i]<r1[i]){
          sum2+=n-i;
       }
       else{
          sum2+=(s1.size()-(s1.lower_bound(l1[i]+1)-s1.begin()+1) );
       }
   }
   cout<<sum1+sum2<<"\n";
   return 0;
}
